package com.liohi.ThreadPool;


/**
 * 当前线程变量
 */
public class AppRuntimeEnv implements AutoCloseable {

    private static final ThreadLocal<AppRuntimeContext> APP_RUNTIME_CONTEXT_THREAD_LOCAL = ThreadLocal.withInitial(AppRuntimeContext::new);


    /**
     * 清除线程环境变量
     */
    public static void clearContext() {
        APP_RUNTIME_CONTEXT_THREAD_LOCAL.remove();
    }

    public static AppRuntimeContext getContext() {
        return APP_RUNTIME_CONTEXT_THREAD_LOCAL.get();
    }

    public static void setContext(AppRuntimeContext appRuntimeContext) {
        APP_RUNTIME_CONTEXT_THREAD_LOCAL.set(appRuntimeContext);
    }

    @Override
    public void close() throws Exception {
        APP_RUNTIME_CONTEXT_THREAD_LOCAL.remove();
    }
}
